package com.onpositive.mediawiki.serializing;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import com.onpositive.data.serializer.writers.ByteArrayWriter;
import com.onpositive.units.ParsedSection;
import com.onpositive.units.TextContent;


public class TextContentWriter extends AbstractMediaWikiWriter<TextContent> {

	ByteArrayWriter sw = new ByteArrayWriter() ;
	private static final int INT_SIZE = 4 ;
	
	public TextContentWriter() {
		super(TextContent.class);
	}

	@Override
	public void append(TextContent tc ) {
		if (tc==null){
			tc=new TextContent("");
		}
//		MarkedTree tree = new MarkedTree() ;
//		MarkedVertex root = tree.getRoot() ;
//		ArrayList<String> textContent = new ArrayList<String>() ;
//		passVertex( obj, root, textContent ) ;
//		
//		int[] treeSpecifiationArray = tree.getSpecificationArray() ;		
//		int size = tree.getSize() ;
//		ArrayList<byte[]> byteContent = new ArrayList<byte[]>( size ) ;
//		int[] byteContentOffsets = new int[ size+1 ] ;
//		
//		int treeStructureContentSize = treeSpecifiationArray.length ;			
//		int currentOffset = INT_SIZE * ( treeStructureContentSize + size + 1 );
//		byteContentOffsets[0] = currentOffset ;
//		try {
//			for( int i = 0 ; i < size ; i++ )
//			{
//				Object o = tree.getVertex(i).getMark() ;
//				if( !(o instanceof String) )
//					throw new IllegalStateException("In this tree all marks should be Strings.") ;
//				
//				String str = (String) o ;
//				byte[] byteArray;				
//				byteArray = str.getBytes("UTF-8");
//				byteContent.add(byteArray) ;
//				currentOffset += byteArray.length ;
//				byteContentOffsets[i+1] = currentOffset ; 			
//			}
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		int totalSize = INT_SIZE + currentOffset ;
//		
//		ByteBuffer bBuf = ByteBuffer.allocate( totalSize ) ;
//		IntBuffer iBuf = bBuf.asIntBuffer() ;
//		iBuf.put( totalSize ) ;
//		iBuf.put( treeSpecifiationArray ) ;
//		iBuf.put( byteContentOffsets ) ;
//		for( int i = 0 ; i < size ; i++ )
//			bBuf.put( byteContent.get(i) ) ;
//		
//		byte[] totalContent = new byte[ totalSize ] ;
//		bBuf.get( totalContent ) ;
		
		StringBuilder builder = new StringBuilder() ;
		ArrayList<ParsedSection> sectionsList = new ArrayList<ParsedSection>( tc.getSections() );
		int s = sectionsList.size() ;
		for( int i = 0 ; i < s ; i++ )
		{
			builder.append( sectionsList.get(i).getValueAsString() ) ;
			if( i < s-1 )
				builder.append( TextContent.SECTION_SEPARATOR ) ;
		}
		
		try {
			sw.append( builder.toString().getBytes("UTF-8") ) ;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			sw.append( builder.toString().getBytes() ) ;
		}
		
	}

	@Override
	public void serialize(DataOutputStream stream) throws IOException {
		sw.serialize(stream) ;	
	}
	
	
	
//	private void passVertex( TextContent tc , MarkedVertex vert, ArrayList<String> textContent )
//	{
//		Object obj = tc.value() ;
//		if( obj != null )
//		{
//			String str = (String)obj ;
//			vert.setMark(str) ;
//		}
//		
//		HashMap<String, ParsedProperty> subSections = tc.getSubsections() ;
////		for( TextContent ps : subSections.values() )
////		{
////			MarkedVertex v = new MarkedVertex() ;
////			vert.addChild(v) ;
////		}
//	}

}
